<template>
  <a-modal
    :title="formInfo.id==undefined?'新增':'修改'"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :destroyOnClose="true"
    @ok="handleSubmit"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model
        ref="ruleForm"
        :model="formInfo"
        :label-col="$global.labelCol"
        :wrapper-col="$global.wrapperCol"
      >
        <a-row>
          <a-col :span="20">
            <a-form-model-item
              label="岗位名称"
              prop="name"
              :rules="$valid.required"
              :maxLength="50">
              <a-input v-model="formInfo.name" placeholder="请输入岗位名称"/>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="20">
            <a-form-model-item
              label="岗位排序"
              prop="sortOrder"
              :rules="$valid.required"
              :maxLength="50">
              <a-input type="number" min="0" v-model="formInfo.sortOrder" placeholder="请输入岗位排序"/>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="20">
            <!--            <a-form-model-item-->
            <!--              label="部门名称"-->
            <!--              prop="departName"-->
            <!--              :rules="$valid.required"-->
            <!--              :maxLength="50">-->
            <!--              <a-input v-model="formInfo.departName" placeholder="请输入部门名称"/>-->
            <!--            </a-form-model-item>-->
            <a-form-model-item
              label="归属部门"
              prop="departCode"
              :rules="$valid.required"
            >
              <a-input
                v-model="formInfo.departCode"
                placeholder="请输入"
                type="hidden"/>
              <depart-window-show
                v-if="visible"
                :org-flag="-1"
                :multiple-flag="false"
                :checkedDepartKeys="departCode"
                @ok="departOk"
              ></depart-window-show>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
  import { getAction, postAction } from '@/api/manage'
  import DepartWindowShow from '@/components/WxDepartChoose/DepartWindowShow'
  // 引入搜索部门弹出框的组件
  // import { getCompanyInfo, getUserRoleAll, saveCompanyInfo } from '@/api/api'
  export default {
    name: 'DepartmentPostionEdit',
    components: {
      DepartWindowShow
    },
    data () {
      return {
        visible: false,
        confirmLoading: false,
        formInfo: {
          name: undefined,
          sortOrder: undefined,
          departCode: undefined
        },
        departCode: undefined,
        url: {
          saveInfo: '/system/departmentPostion/save',
          getInfo: '/system/departmentPostion/info'
        }
      }
    },
    created () {
    },
    methods: {
      add () {
        this.departCode = undefined
        this.edit()
      },
      edit (record) {
        this.$objToOverride(this.formInfo, record)
        if (this.formInfo.id) {
          getAction(this.url.getInfo, { 'id': record.id }).then(res => {
            this.$nextTick(() => {
              this.formInfo = res.result.data
              this.departCode = this.formInfo.departCode
            })
          })
        }
        this.visible = true
      },
      // 确定
      handleSubmit () {
        // 触发表单验证
        this.confirmLoading = true
        this.formInfo.departCode = this.departCode
        this.$refs.ruleForm.validate(valid => {
          if (valid) {
            postAction(this.url.saveInfo, this.formInfo).then(res => {
              this.$message.success(res.content)
              this.$emit('ok', this.formInfo)
              this.close()
            }).finally(() => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      // 关闭
      handleCancel () {
        this.close()
      },
      close () {
        this.$objToReset(this.formInfo)

        this.$emit('close')
        this.visible = false
      },
      editorOk (val) {
        this.formInfo.content = val
      },
      // 获取用户对应部门弹出框提交给返回的数据
      departOk (val) {
        this.departCode = val
        console.log('this.departCode=' + this.departCode)
      }
    }
  }
</script>
